package com.bosssoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.entity.dto.FibRoleAuthDTO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibRoleAuthMapper;
import com.bosssoft.entity.po.FibRoleAuthPO;
import com.bosssoft.service.FibRoleAuthService;
/**
 * @author Li kexi
 * Created on 2021/8/16.14:32
 */
@Service
@Slf4j
public class FibRoleAuthServiceImpl extends ServiceImpl<FibRoleAuthMapper, FibRoleAuthPO> implements FibRoleAuthService{

    @Override
    public boolean addRoleAuth(FibRoleAuthDTO roleAuthDTO) {
        try {
            FibRoleAuthPO roleAuthPO = new FibRoleAuthPO();
            BeanUtils.copyProperties(roleAuthDTO, roleAuthPO);
            return this.baseMapper.insert(roleAuthPO) > 0;
        } catch (DaoException e) {
            log.info("addRoleAuth出错");
            //TODO 换异常
        }
        return false;
    }

    @Override
    public boolean modifyRoleAuth(FibRoleAuthDTO roleAuthDTO) {
        try {
            FibRoleAuthPO roleAuthPO = new FibRoleAuthPO();
            BeanUtils.copyProperties(roleAuthDTO, roleAuthPO);
            return this.baseMapper.updateById(roleAuthPO) > 0;
        } catch (DaoException e) {
            log.info("modifyRoleAuth出错");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }

    @Override
    public boolean banRoleAuthById(Long id, Integer status) {
        return this.baseMapper.banRoleAuthById(id, status) > 0;
    }

    @Override
    public boolean deleteRoleAuthById(Long id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public List<Long> getAllAuthIdByRoleId(Long roleId) {
        return this.baseMapper.getAllAuthIdByRoleId(roleId);
    }

    @Override
    public IPage<FibRoleAuthPO> getAllAuthByRoleIdPaging(Long roleId) {
        IPage<FibRoleAuthPO> page = new Page<>(10,2);
        LambdaQueryWrapper<FibRoleAuthPO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(roleId != null, FibRoleAuthPO::getRoleId, roleId);
        return page(page, wrapper);
    }

    @Override
    public int updateBatch(List<FibRoleAuthPO> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<FibRoleAuthPO> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<FibRoleAuthPO> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(FibRoleAuthPO record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(FibRoleAuthPO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }
}
